package cn.edu.tju.fctl.util;

import cn.edu.tju.fctl.service.IUserService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by liutong on 15/12/28.
 */
public class ExcelUtil {
    public static void generateExcel(List<Evaluation> evaluationList){

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFCellStyle dateCellStyle = workbook.createCellStyle();
        short df = workbook.createDataFormat().getFormat("yyyy-MM-dd");
        dateCellStyle.setDataFormat(df);
        HSSFSheet sheet = workbook.createSheet("sheet1");

        HSSFRow user_id_row = sheet.createRow(0);
        user_id_row.createCell(0).setCellValue("用户ID");

        HSSFRow from_date_row = sheet.createRow(1);
        from_date_row.createCell(0).setCellValue("起始时间");

        HSSFRow to_date_row = sheet.createRow(2);
        to_date_row.createCell(0).setCellValue("终止时间");

        HSSFRow out_time_row = sheet.createRow(3);
        out_time_row.createCell(0).setCellValue("外出时间");

        HSSFRow absent_time_row = sheet.createRow(4);
        absent_time_row.createCell(0).setCellValue("缺勤时间");

        HSSFRow over_time_row = sheet.createRow(5);
        over_time_row.createCell(0).setCellValue("加班时间");

        HSSFRow nianjia_row = sheet.createRow(6);
        nianjia_row.createCell(0).setCellValue("年假");

        HSSFRow chanjia_row = sheet.createRow(7);
        chanjia_row.createCell(0).setCellValue("产假");

        HSSFRow hunjia_row = sheet.createRow(8);
        hunjia_row.createCell(0).setCellValue("婚假");

        HSSFRow shijia_row = sheet.createRow(9);
        shijia_row.createCell(0).setCellValue("事假");

        HSSFRow bingjia_row = sheet.createRow(10);
        bingjia_row.createCell(0).setCellValue("病假");

        int columnNum = 1;
        for(Evaluation evaluation : evaluationList){
            user_id_row.createCell(columnNum).setCellValue(evaluation.getUserId());

            from_date_row.createCell(columnNum).setCellStyle(dateCellStyle);
            from_date_row.getCell(columnNum).setCellValue(evaluation.getFromDate());

            to_date_row.createCell(columnNum).setCellStyle(dateCellStyle);
            to_date_row.getCell(columnNum).setCellValue(evaluation.getToDate());

            out_time_row.createCell(columnNum).setCellValue(evaluation.getOutHour());

            absent_time_row.createCell(columnNum).setCellValue(evaluation.getAbsentHour());

            over_time_row.createCell(columnNum).setCellValue(evaluation.getOverHour());

            int rowNum = 6;
            for(double leaveHourItem : evaluation.getLeaveHours()){
                HSSFRow leaveHourRow = sheet.getRow(rowNum++);
                leaveHourRow.createCell(columnNum).setCellValue(leaveHourItem);
            }
            
            columnNum++;
        }

        try {
            FileOutputStream out = new FileOutputStream(new File(System.getProperty("user.dir") + "/fileupload/temp.xls"));
            workbook.write(out);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
